﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProceduralCity.Models
{
    // TODO: Add geometry to the class. So far it only acts as a state bag.
    class ModelState
    {
        // TODO: Add an origin.

        /// <summary>
        /// By convention, this should be the bottom, center of the object.
        /// </summary>
        public Vector3 Position { get; set; }

        /// <summary>
        /// By convention, no rotation should have the object upright (+Y) and facing +Z.
        /// </summary>
        public Vector3 Rotation { get; set; }
        public float Scale { get; set; } // TODO: can this be a Vector3, or would that screw up the transforms?

        // TODO: Have this update with changes to Position, Rotation, and Scale.
        public BoundingBox BoxBounds { get; set; }

        public Vector3 Size
        {
            get
            {
                if (BoxBounds != null)
                    return Scale * (BoxBounds.Max - BoxBounds.Min);
                return Vector3.Zero;
            }
        }

        // TODO: Consider caching the matrix and deleting it when another property is changed.
        public Matrix LocalWorld
        {
            get
            {
                Matrix m = Matrix.CreateScale(Scale) *
                    Matrix.CreateRotationY(Rotation.Y) *
                    Matrix.CreateRotationX(Rotation.X) *
                    Matrix.CreateRotationZ(Rotation.Z) *
                    Matrix.CreateTranslation(Position);
                return m;
            }
        }

        public ModelState()
            : this(Vector3.Zero, Vector3.Zero, 1)
        { }

        public ModelState(Vector3 position, Vector3 rotation, float scale)
        {
            this.Position = position;
            this.Rotation = rotation;
            this.Scale = scale;
        }
    }
}
